/**
 * author:huangyuan email:huangyuan3h@gmail.com jquery plugin about dialog
 * solution
 * 
 * need location="jqueryAjax.html" title="jqueryajax"
 * 
 * need 3 file JqDialog,js jQDIalog.css Dialog-icon.png depend on jquery ui
 * dialog object:window.JqDialog["id"]
 * 
 */

(function($) {

	window.JqDialog = new Array();// define a place to store dialog

	var JqDialog = function(o, sender) {
		var $t = this;
		this.link = $(sender);// the link you click
		this.Dialog = null;// dialog jquery object
		this._o = o;// option
		this.key = $t.link.attr("id");// key
		this.container = $(o["container"]);

		// main in
		$t.link.click(function() {
			var hasInit = init();
			if (!hasInit) {
				initDraggableAndResizable();
				InitMin();
				initMax();
				initClose();
				DialogFocus();
			}

		});

		// recode it self

		$t.record = function() {
			$t.Dialog.attr("resleft", $t.Dialog.css("left"));
			$t.Dialog.attr("restop", $t.Dialog.css("top"));
			$t.Dialog.attr("reswidth", $t.Dialog.css("width"));
			$t.Dialog.attr("resheight", $t.Dialog.css("height"));
			return $t.Dialog;
		};

		// show it self
		$t.show = function() {
			$t.Dialog.show().addClass("dialog-focus").trigger("mousedown");
			$t.Dialog.animate({
				top : $t.Dialog.attr("restop"),
				left : $t.Dialog.attr("resleft"),
				width : $t.Dialog.attr("reswidth"),
				height : $t.Dialog.attr("resheight")
			}, 500);
			return $t.Dialog;
		};

		// init dialog
		var init = function() {

			if (window.JqDialog[$t.link.attr("id")] != undefined && window.JqDialog[$t.link.attr("id")] != null) {

				$t.show();
				return true;

			} else if (window.JqDialog[$t.link.attr("id")] === undefined) {
				// support 2 way to set location and title
				if ($t.link.attr("location") != undefined) {
					$t._o["location"] = $t.link.attr("location");
				}

				if ($t.link.attr("title") != undefined) {
					$t._o["title"] = $t.link.attr("title");
				}

				var str = "<div id='"
						+ $t.key
						+ "_dialog' class='dialog' "
						+ "key='"
						+ $t.key
						+ "'  resleft restop  reswidth  resheight  >"
						+ "<div class='releaseHelp'></div>"
						+ "<div class='dialogHead'>"
						+ $t._o["title"]
						+ "<div class='iconDiv'> "
						+ "<a class='Dialog-mini'></a>"
						+ "<a class='Dialog-max' icon='max'></a>"
						+ "<a class='Dialog-close'></a>"
						+ "</div>"
						+ "</div>"
						+ "<div class='locationDiv'><span>Location:</span><input type='text' id='location' disabled value=''></div>"
						+ "<div class='dialogContent'>" + "<iframe src='" + $t._o["location"] + "?key=" + $t.key
						+ "' class='dialogBody' name='" + $t.key
						+ "Iframe' webkitAllowFullScreen mozAllowFullScreen allowFullScreen>" + "</iframe>" + "</div>"
						+ "</div>";

				$t.container.append(str);

				$t.Dialog = window.JqDialog[$t.key] = $("#" + $t.key + "_dialog");
				$t.Dialog.addClass("dialog-focus");
				$t._o['init']($t);

				$t.record();
				$t.Dialog.find(".releaseHelp").on("mousedown  click ", function() {
					$(this).hide();
				});

				$t.Dialog.find("#location").val($t.Dialog.find("iframe")[0].src);

				$t.Dialog.find(".dialogHead").dblclick(function() {
					if ($t.Dialog.find(".Dialog-max").length == 1) {
						$t.Dialog.find(".Dialog-max").trigger("mousedown");
					} else {
						$t.Dialog.find(".Dialog-restore").trigger("mousedown");
					}

				});

				// end of initial
				return false;
			}

		};

		// /////initial draggable and resizable
		var initDraggableAndResizable = function() {

			$(".releaseHelp").hide();
			$t.Dialog.draggable({
				containment : "parent",
				handle : ".dialogHead",
				scroll : false,
				start : function() {
					$(".releaseHelp").show();
				},
				stop : function() {
					$t.Dialog.find(".releaseHelp").hide();
					$t.record();
				}
			}).resizable({
				start : function() {
					$(".releaseHelp").show();
				},
				stop : function() {
					$(".releaseHelp").hide();
					$t.record();

				}
			}).click(function() {

				$t._o['click']();
			}).mousedown(function() {
				$t._o['mousedown']();
			});
		};// end of initDraggableAndResizable

		var InitMin = function() {
			$t.Dialog.find(".Dialog-mini").mouseenter(function() {
				$(this).addClass("Dialog-mini-over");
			}).mouseleave(function() {
				$(this).removeClass("Dialog-mini-over");
			}).mousedown(function() {
				if ($t.Dialog.fullScreen() == false) {
					$t.record();
				} else if ($(document).fullScreen() != null) {
					$t.Dialog.fullScreen(false);
				}

				$t._o['min']($t.Dialog);
			});

		};// end of init min

		// this function helps max works
		function maxIconClick() {
			$(this).removeClass("Dialog-mini-over").unbind("mousedown").mousedown(maxIconClick);
			_s = $t.Dialog;

			if ("max" == $(this).attr("icon")) {
				$t.record();
				$(this).removeClass("Dialog-max-over").removeClass("Dialog-restore-over");

				$(this).attr("icon", "res").removeClass("Dialog-max").addClass("Dialog-restore")
				//
				.mouseenter(function() {
					$(this).addClass("Dialog-restore-over");
				}).mouseleave(function() {
					$(this).removeClass("Dialog-restore-over");
				});
				_s.draggable("destroy").resizable("destroy");

				_s.animate({
					top : "0px",
					left : "0px",
					width : "98%",
					height : "98%"
				}, 500);
				if ($(document).fullScreen() != null) {
					_s.fullScreen(true);
				}

				$t._o['max']();

			} else if ("res" == $(this).attr("icon")) {

				$(this).removeClass("Dialog-max-over").removeClass("Dialog-restore-over").unbind("mouseenter");

				$(this).attr("icon", "max").removeClass("Dialog-restore").addClass("Dialog-max")

				.mouseenter(function() {
					$(this).addClass("Dialog-max-over");

				}).mouseleave(function() {
					$(this).removeClass("Dialog-max-over");
				});

				initDraggableAndResizable(_s);

				$t.show();
				if ($(document).fullScreen() != null) {
					_s.fullScreen(false);
				}
			}

		}

		var initMax = function() {
			$t.Dialog.find(".Dialog-max").mouseenter(function() {
				$(this).addClass("Dialog-max-over");
			}).mouseleave(function() {
				$(this).removeClass("Dialog-max-over");
			}).mousedown(maxIconClick);

		};

		// control close icon
		var initClose = function() {

			$t.Dialog.find(".Dialog-close").mouseenter(function() {
				$(this).addClass("Dialog-close-over");

			}).mouseleave(function() {
				$(this).removeClass("Dialog-close-over");
			}).mousedown(function() {
				$t._o['close']($t);
				$t.Dialog.fadeOut(500, function() {
					delete window.JqDialog[$(this).attr("key")];
					$(this).remove();
				});

			});

		};

		// ////// windows is focus
		var DialogFocus = function() {
			$t.Dialog.mousedown(function() {
				for ( var key in window.JqDialog) {
					window.JqDialog[key].removeClass("dialog-focus");
				}

				$(this).addClass("dialog-focus");

			});

		};

	};

	$.fn.JqDialog = function(o) {
		var _default = {
			location : "",
			title : 'dialog title',
			container : 'body',
			init : function() {
			},
			close : function() {
			},
			max : function() {
			},
			min : function() {
			},
			click : function() {
			},
			mousedown : function() {
			}
		};
		this.option = $.extend(_default, o);
		return new JqDialog(this.option, this);
	};

})(jQuery);
